#include <cstdio>

#define N 1001

int tree[N];

int find_root(int x);

int main(int argc, char const *argv[]){
    int Times;
    scanf("%d", &Times);
    for (int i = 0; i < Times; i++){
        int n, m;
        scanf("%d %d", &n, &m);
        for (int j = 1; j <= n; j++)
            tree[j] = -1;
        for (int j = 0; j < m; j++){
            int a, b;
            scanf("%d %d", &a, &b);
            a = find_root(a);
            b = find_root(b);
            if (a != b){
                tree[a] = b;
            }
        }
        int result = 0;
        for (int j = 1; j <= n; j++){
            if (tree[j] == -1)
                result++;
        }
        printf("%d\n", result);
    }
    return 0;
}

int find_root(int x){
    if (tree[x] == -1)
        return x;
    else {
        int temp_index = find_root(tree[x]);
        tree[x] = temp_index;
        return temp_index;
    }
}